package edu.vt.ece_3574.amber_alert.curfew;

import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import edu.vt.ece_3574.amber_alert.R;

/**
 * This class handles the MapView component on the Curfew screen
 * 
 * @author acorvin
 */
public class CurfewLocationSelectorHandler extends MapActivity {
	/** The currently selected location on the map */
	private GeoPoint selectedLocation;
	
	private Resources resources;
	
	public CurfewLocationSelectorHandler(MapView mapView, Resources resources) {
		this.resources = resources;
		
		//Add an overlay to the MapView to handle creating a marker on the map wherever you tap
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay); 
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	/**
	 * Returns the location that is selected on the map
	 * @return selectedLocation
	 */
	public GeoPoint getSelectedLocation() {
		return selectedLocation;
	}
	
	class MapOverlay extends com.google.android.maps.Overlay {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when)  {
            super.draw(canvas, mapView, shadow);                   
 
            if (selectedLocation != null) {
            	//---translate the GeoPoint to screen pixels---
            	Point screenPts = new Point();
            	mapView.getProjection().toPixels(selectedLocation, screenPts);

            	//---add the marker---
				Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.pushpin);            
            	canvas.drawBitmap(bmp, screenPts.x - 20, screenPts.y-45, null);  
            }
            return true;
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {   
            //---when user lifts his finger get the location on the map that was touched
            if (event.getAction() == 1) {                
                selectedLocation = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
            }
            return false;
        }
    }
}
